{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>price</th>\n",
       "      <th>frequency</th>\n",
       "      <th>RAM</th>\n",
       "      <th>size</th>\n",
       "      <th>disk</th>\n",
       "      <th>SSD</th>\n",
       "      <th>GPU</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18200</td>\n",
       "      <td>2.6</td>\n",
       "      <td>16</td>\n",
       "      <td>15.4</td>\n",
       "      <td>512</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4999</td>\n",
       "      <td>1.8</td>\n",
       "      <td>4</td>\n",
       "      <td>13.6</td>\n",
       "      <td>512</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3699</td>\n",
       "      <td>1.6</td>\n",
       "      <td>4</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1024</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9999</td>\n",
       "      <td>1.6</td>\n",
       "      <td>8</td>\n",
       "      <td>14.0</td>\n",
       "      <td>256</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>8599</td>\n",
       "      <td>2.2</td>\n",
       "      <td>8</td>\n",
       "      <td>15.6</td>\n",
       "      <td>1024</td>\n",
       "      <td>no</td>\n",
       "      <td>yes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>23000</td>\n",
       "      <td>2.2</td>\n",
       "      <td>32</td>\n",
       "      <td>15.6</td>\n",
       "      <td>1024</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>4699</td>\n",
       "      <td>2.3</td>\n",
       "      <td>8</td>\n",
       "      <td>13.3</td>\n",
       "      <td>256</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2999</td>\n",
       "      <td>2.6</td>\n",
       "      <td>4</td>\n",
       "      <td>13.0</td>\n",
       "      <td>256</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   price  frequency  RAM  size  disk  SSD  GPU\n",
       "0  18200        2.6   16  15.4   512  yes  yes\n",
       "1   4999        1.8    4  13.6   512  yes  yes\n",
       "2   3699        1.6    4  13.0  1024   no   no\n",
       "3   9999        1.6    8  14.0   256  yes   no\n",
       "4   8599        2.2    8  15.6  1024   no  yes\n",
       "5  23000        2.2   32  15.6  1024  yes  yes\n",
       "6   4699        2.3    8  13.3   256  yes   no\n",
       "7   2999        2.6    4  13.0   256  yes   no"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df=pd.read_csv('笔记本集合.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基于实例的推荐\n",
    "\n",
    "# 输入p_r表示项目值，r表示对应需求特征值的集合，r_val表示目标值\n",
    "def get_MIB(p_r,r):\n",
    "    return (p_r-min(r))/(max(r)-min(r))\n",
    "def get_LIB(p_r,r):\n",
    "    return (max(r)-p_r)/(max(r)-min(r))\n",
    "def get_CIB(p_r,r,r_val):\n",
    "    return 1-np.abs(p_r-r_val)/(max(r)-min(r))\n",
    "\n",
    "# 输入 REQ={price=8000, size=14, RAM=8, GPU=yes}\n",
    "# 相似度类别：{price:LIB, size:CIB, RAM:MIB, GPU:0-1匹配}\n",
    "# 权重：{price:1.0, size:0.5, RAM:0.8, GPU:1.0}\n",
    "\n",
    "# 根据输入制定规则，计算匹配度\n",
    "def get_match(p):\n",
    "    score=0\n",
    "    score+=get_LIB(p['price'],df['price'])*1.0\n",
    "    score+=get_CIB(p['size'],df['size'],14)*0.5\n",
    "    score+=get_MIB(p['RAM'],df['RAM'])*0.8\n",
    "    score+= (1 if p['GPU']=='yes' else 0)*1.0\n",
    "    return score/(1.0+0.5+0.8+1.0)\n",
    "\n",
    "df['match']=df.apply(lambda x:get_match(x),axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>price</th>\n",
       "      <th>frequency</th>\n",
       "      <th>RAM</th>\n",
       "      <th>size</th>\n",
       "      <th>disk</th>\n",
       "      <th>SSD</th>\n",
       "      <th>GPU</th>\n",
       "      <th>match</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18200</td>\n",
       "      <td>2.6</td>\n",
       "      <td>16</td>\n",
       "      <td>15.4</td>\n",
       "      <td>512</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>0.549580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4999</td>\n",
       "      <td>1.8</td>\n",
       "      <td>4</td>\n",
       "      <td>13.6</td>\n",
       "      <td>512</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>0.703964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3699</td>\n",
       "      <td>1.6</td>\n",
       "      <td>4</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1024</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>0.385665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9999</td>\n",
       "      <td>1.6</td>\n",
       "      <td>8</td>\n",
       "      <td>14.0</td>\n",
       "      <td>256</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>0.383122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>8599</td>\n",
       "      <td>2.2</td>\n",
       "      <td>8</td>\n",
       "      <td>15.6</td>\n",
       "      <td>1024</td>\n",
       "      <td>no</td>\n",
       "      <td>yes</td>\n",
       "      <td>0.614123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>23000</td>\n",
       "      <td>2.2</td>\n",
       "      <td>32</td>\n",
       "      <td>15.6</td>\n",
       "      <td>1024</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>0.603730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>4699</td>\n",
       "      <td>2.3</td>\n",
       "      <td>8</td>\n",
       "      <td>13.3</td>\n",
       "      <td>256</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>0.422629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2999</td>\n",
       "      <td>2.6</td>\n",
       "      <td>4</td>\n",
       "      <td>13.0</td>\n",
       "      <td>256</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>0.396270</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   price  frequency  RAM  size  disk  SSD  GPU     match\n",
       "0  18200        2.6   16  15.4   512  yes  yes  0.549580\n",
       "1   4999        1.8    4  13.6   512  yes  yes  0.703964\n",
       "2   3699        1.6    4  13.0  1024   no   no  0.385665\n",
       "3   9999        1.6    8  14.0   256  yes   no  0.383122\n",
       "4   8599        2.2    8  15.6  1024   no  yes  0.614123\n",
       "5  23000        2.2   32  15.6  1024  yes  yes  0.603730\n",
       "6   4699        2.3    8  13.3   256  yes   no  0.422629\n",
       "7   2999        2.6    4  13.0   256  yes   no  0.396270"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
